<template>
  <div>
    <el-card>
      <!-- 搜索框与添加按钮 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="pageInfo.query" clearable>
            <el-button
              :loading="searchStatus"
              slot="append"
              icon="el-icon-search"
              @click="getGoodsCateInfo"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" icon="el-icon-edit" @click="isAddGoods"
            >添加商品</el-button
          >
        </el-col>
      </el-row>
      <!-- 用户信息表格 -->
      <el-row>
        <el-table
          :data="allGoodsCateList.goodsCateList"
          border
          stripe
          style="width: 100%"
        >
          <el-table-column
            type="index"
            label="#"
            width="80px"
          ></el-table-column>
          <el-table-column
            prop="goods_name"
            label="商品名称"
            min-width="450px"
          ></el-table-column>
          <el-table-column
            prop="goods_price"
            label="价格（元）"
            min-width="120px"
          ></el-table-column>
          <el-table-column
            prop="goods_weight"
            label="重量"
            min-width="120px"
          ></el-table-column>
          <el-table-column
            prop="add_time"
            label="创建时间"
            min-width="180px"
          >
          <template v-slot="scope">
            {{scope.row.add_time | dateFormat}}
          </template>
          </el-table-column>
          <el-table-column label="操作" min-width="300px">
            <template v-slot="scope">
              <el-button
                @click="isChangeGoods(scope.row.goods_id)"
                type="primary"
                icon="el-icon-edit"
                size="small"
                >编辑</el-button
              >
              <el-button
                @click="delGoodsInfo(scope.row.goods_id)"
                type="danger"
                icon="el-icon-delete"
                size="small"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-row>
      <el-row>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="pageInfo.pagenum"
          :page-sizes="[2, 5, 10, 20]"
          :page-size="pageInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="allGoodsCateList.total"
        >
        </el-pagination>
      </el-row>
    </el-card>
  </div>
</template>

<script>
import { getGoodsInfoApi, delGoodsInfoApi } from '@/Utils/Api/gitGoodsInfoApi'
export default {
  name: 'CategoriesView',
  data () {
    return {
      searchStatus: false,
      searchInfo: '',
      pageInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      allGoodsCateList: {
        goodsCateList: [],
        total: 0
      },
      thisUserInfoId: 0
    }
  },
  components: {
  },
  created () {
    this.getGoodsCateInfo()
  },
  mounted () {},
  methods: {
    isAddGoods () {
      this.$router.push('/goods/addGoods')
    },
    isChangeGoods (id) {
      this.$nextTick(() => {
        this.$message({
          type: 'info',
          message: '懒得写这个功能了' + '这个地方的id是' + id
        })
      })
    },
    // 获得所有商品信息列表
    async getGoodsCateInfo () {
      this.searchStatus = true
      try {
        const { data } = await getGoodsInfoApi(this.pageInfo)
        console.log(data)
        if (data.meta.status === 200) {
          this.allGoodsCateList.goodsCateList = data.data.goods
          this.allGoodsCateList.total = data.data.total
          this.$message({
            type: 'success',
            message: '表单数据已更新'
          })
          this.searchStatus = false
        } else {
          return this.$message({
            type: 'error',
            message: '表单获取失败'
          })
        }
      } catch (error) {
        return console.log(error)
      }
    },
    // 删除用户信息
    async delGoodsInfo (id) {
      console.log(id)
      const confirmRus = await this.$confirm('此操作将永久删除用户?', '警告!', {
        distinguishCancelAndClose: true,
        confirmButtonText: '删除',
        cancelButtonText: '取消删除'
      }).catch((err) => err)
      console.log(confirmRus)
      if (confirmRus !== 'confirm') {
        return this.$message({
          type: 'info',
          message: '用户取消了删除'
        })
      }
      const { data } = await delGoodsInfoApi(id)
      console.log(data)
      if (data.meta.status !== 200) {
        return this.$message({
          type: 'error',
          message: data.meta.msg
        })
      }
      this.getGoodsCateInfo()
      this.$message({
        type: 'success',
        message: data.meta.msg
      })
    },
    // 分页
    handleSizeChange (val) {
      this.pageInfo.pagesize = val
      this.getGoodsCateInfo()
    },
    handleCurrentChange (val) {
      this.pageInfo.pagenum = val
      this.getGoodsCateInfo()
    }
  },
  computed: {},
  watch: {}
}
</script>

<style lang="less" scoped>
.el-table {
  margin-top: 15px;
}
</style>
